#include "BusinessDNA.h"
#include "Globle.h"
#include <string>
#include "OS.h"


using namespace std;

CBusinessDNA::CBusinessDNA()
{
	m_AcessType = A_UNKOWN;
	m_ServiceType = S_UNKOWN;
	m_FromWhere = W_UNKOWN;
	m_FileFormat = F_UNKOWN;
	m_NetProtocol = P_UNKOWN;
}

CBusinessDNA::CBusinessDNA(const CBusinessDNA &other)
{
	*this = other;
}

CBusinessDNA& CBusinessDNA::operator = (const CBusinessDNA &other)
{
	if(this == &other)
		return *this;

	m_cUrl = other.m_cUrl;
	m_ClientType = other.m_ClientType;
	m_AcessType = other.m_AcessType;
	m_ServiceType = other.m_ServiceType;
	m_FromWhere = other.m_FromWhere;
	m_FileFormat = other.m_FileFormat;
	m_NetProtocol = other.m_NetProtocol;
	m_TopModel = other.m_TopModel;
	return *this;
}

Bool CBusinessDNA::Parser(const string &strUrl, EClientType eClientType, Bool isRandAcess)
{
	if(!m_cUrl.ParserUrl(strUrl))
		return FALSE;
	
	return Parser(m_cUrl, eClientType, isRandAcess);
}

Bool CBusinessDNA::Parser(const CUrlParser &cUrl, EClientType eClientType, Bool isRandAcess)
{
	if(&m_cUrl != &cUrl)
		m_cUrl = cUrl;
	
	//Client Type
	m_ClientType = eClientType;

	//AcessType
	m_AcessType = isRandAcess ? A_RANDOM : A_UNRAND;

	//BusinessType
	string strExtName = m_cUrl.GetExtName();
	if(strExtName == "smil" ||
	   strExtName == "sdp" ||
	   strExtName == "sml")
	{
		m_ServiceType = S_LIVE;
	}
	else if(strExtName == "mp4" ||
			strExtName == "wmv" ||
			strExtName == "asf" ||
			strExtName == "rm" ||
			strExtName == "rmvb" ||
			strExtName == "3gp" ||
			strExtName == "3g2")
	{
		m_ServiceType = S_VOD;
	}
	else
	{
		return FALSE;
	}

	//FileFormat
	m_FileFormat = ExtNameToFileFormat(strExtName);
	if(m_FileFormat == F_UNKOWN)
		return FALSE;
	
	
	//NetProtocol
	string strProtocol = m_cUrl.GetProtocolName();
	m_NetProtocol = ProtoNameToNetProto(strProtocol);
	if(m_NetProtocol == P_UNKOWN)
		return FALSE;
	
	
	string strFullPath = m_cUrl.GetFullPath();
	if(OS::IsFileExist(strFullPath.c_str()))
	{
		m_FromWhere = W_LOCAL;
	}
	else
	{
		if(m_NetProtocol == P_FILE)
			return FALSE;
		else
			m_FromWhere = W_NET;
	}
	

	//Top Model
	if(SConfigPara::m_isWithP2PMode)
		m_TopModel = T_P2P;
	else
		m_TopModel = T_CS;

	return TRUE;
}


EFileFormat CBusinessDNA::ExtNameToFileFormat(const string &strExtName)
{
	if(strExtName == "mp4")
		return F_MP4;
	else if(strExtName == "wmv")
		return F_WMV;
	else if(strExtName == "asf")
		return F_ASF;
	else if(strExtName == "rm" || strExtName == "rmvb")
		return F_RM;
	else if(strExtName == "3gp")
		return F_3GP;
	else if(strExtName == "3g2")
		return F_3G2;
	else if(strExtName == "smil")
		return F_FLV;
	else if(strExtName == "sdp")
		return F_SDP;

	return F_UNKOWN;
}

ENetProtocol CBusinessDNA::ProtoNameToNetProto(const string &strProtoName)
{
	if(strProtoName == "fqtp")
		return P_RSTP;
	else if(strProtoName == "rtsp" || strProtoName == "rrtsp")
		return P_RTSP;
	else if(strProtoName == "http")
		return P_HTTP;
	else if(strProtoName == "mms")
		return P_MMS;
	else if(strProtoName == "file")
		return P_FILE;

	return P_UNKOWN;
}

string CBusinessDNA::operator[](const string &strExtendKey)
{
	return m_cUrl[strExtendKey];
}
